import org.apache.commons.io.FileUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2016/5/16.
 */
public class BaiDuPic2 {
    public static void main(String[] args) throws IOException {
        String url="http://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fr=&sf=1&fmq=1461833981476_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%E5%A3%81%E7%BA%B8#z=0&pn=&ic=0&st=-1&face=0&s=0&lm=-1";
        Connection.Response response = Jsoup.connect(url).execute();
        Pattern pattern = Pattern.compile("\"objURL\":.*\\.jpg");
        Matcher matcher = pattern.matcher(response.body());
        int size=0;
       while(matcher.find()){
//           System.out.println(matcher.group());
           String temp = matcher.group();
           Pattern pattern1 = Pattern.compile("http.*\\.jpg");
//           System.out.println(temp);
           Matcher matcher1 = pattern1.matcher(temp);
           while (matcher1.find()) {
               Connection.Response connection2;
               try {
                   connection2 = Jsoup.connect(matcher1.group()).ignoreContentType(true).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36").execute();
               }catch (Exception e){
                   e.printStackTrace();
                   continue;
               }
               File file = new File("D:\\dvp\\workspace\\pic\\"+size+".jpg");
               FileUtils.writeByteArrayToFile(file,connection2.bodyAsBytes());
               size++;
           }
       }

    }
}
